//
//  STAddCardViewController.m
//  KunLun
//
//  Created by Mac on 2017/11/27.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "STAddCardViewController.h"
#import "STBankModel.h"
#define bankeDes @"请选择银行卡"
@interface STAddCardViewController ()
@property(nonatomic, strong) UITextField                     *zhihangTextField;
@property(nonatomic, strong) UITextField                     *nameTextField;
@property(nonatomic, strong) UITextField                     *idCardTextField;
@property(nonatomic, strong) STButton                        *bankButton;//银行
@property(nonatomic, strong) NSMutableArray                         *bankArray;

@property(nonatomic, strong) NSArray <STTableEasyModel*>                    *datasouce;
@property(nonatomic, strong) NSMutableArray                     *banksArray;
@property(nonatomic, strong) STBankModel                     *chosedBankModel;
@end

@implementation STAddCardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"添加银行卡";
    [self configSubView];
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [SVProgressHUD showInfoWithStatus:@"一旦银行卡绑定成功，不可修改和删除"];
    });
    self.datasouce = @[
                       [[STTableEasyModel alloc]initWithTextString:@"银行名称"
                                                     accessoryView:self.zhihangTextField],
                       [[STTableEasyModel alloc]initWithTextString:@"卡号" accessoryView:self.idCardTextField],
                        [[STTableEasyModel alloc]initWithTextString:@"持卡人"
                                                      accessoryView:self.nameTextField]
                       ];
  //  [self sendfetchBanksRequest];

}

#pragma mark --subView
- (void)configSubView{
    __weak typeof(self) weakSelf =  self;
    STButton * desButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 50)
                                            title:@"请绑定持卡人本人的银行卡"
                                       titleColor:FirstTextColor
                                        titleFont:15
                                     cornerRadius:0
                                  backgroundColor:self.tableView.backgroundColor
                                  backgroundImage:nil
                                            image:nil];
    [desButton setTitleEdgeInsets:UIEdgeInsetsMake(-5, 15, 0, 0)];
    self.tableView.tableHeaderView = desButton;
    
    
   self.bankButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth *0.6, 44)
                                                     title:bankeDes
                                                titleColor:TM_thirdTextColor
                                                 titleFont:15
                                              cornerRadius:0
                                           backgroundColor:nil
                                           backgroundImage:nil
                                                     image:nil];
    [self.bankButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedBankTypeButton];
    }];
    self.zhihangTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth *0.6, 44)];
    self.zhihangTextField.keyboardType = UIKeyboardTypeDefault;
    self.zhihangTextField.placeholder = @"请输入银行名称";
    self.zhihangTextField.textColor = FirstTextColor;
    
    
    self.idCardTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth *0.6, 44)];
    self.idCardTextField.keyboardType = UIKeyboardTypeNumberPad;
    self.idCardTextField.placeholder = @"银行卡卡号";
    self.idCardTextField.textColor = FirstTextColor;
    
    self.nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth *0.6, 44)];
    self.nameTextField.placeholder = @"姓名";
    self.nameTextField.textColor = FirstTextColor;
    
    
    UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    footer.backgroundColor = self.tableView.backgroundColor;
    

    STButton * confimButton = [[STButton alloc] initWithFrame:CGRectMake(30, 0, UIScreenWidth - 60 , 44)
                                                        title:@"确认"
                                                   titleColor:[UIColor whiteColor]
                                                    titleFont:18
                                                 cornerRadius:10
                                              backgroundColor:TM_orangeColor
                                              backgroundImage:nil
                                                        image:nil];
    [confimButton setClicAction:^(UIButton *sender) {
        [weakSelf onSlectedCommitButton];
    }];
    confimButton.top = 20;
    confimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [footer addSubview:confimButton];
    footer.height = confimButton.bottom;
    self.tableView.tableFooterView = footer;
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.datasouce.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    cell.textLabel.textColor = UIColorFromRGBA(0x333333);
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:13];
    cell.detailTextLabel.textColor = UIColorFromRGBA(0x999999);
    
    cell.textLabel.text = [NSString stringWithFormat:@"%lu",indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryNone;
    
    cell.textLabel.text = self.datasouce[indexPath.row].textString;
    cell.accessoryView = self.datasouce[indexPath.row].accessoryView;
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --Action Method
- (void)onSlectedCommitButton{
    
    if (!self.zhihangTextField.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入银行名称"];
        return;
    }
    if (!self.idCardTextField.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入卡号"];
        return;
    }
    if (!self.nameTextField.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入姓名"];
        return;
    }
    [self sendAddbankRequest];
    
}
- (void)onSelectedBankTypeButton{
    if (!self.banksArray.count) {
        [SVProgressHUD showErrorWithStatus:@"暂无银行信息"];
        return;
    }
    [self.zhihangTextField resignFirstResponder];
    [self.idCardTextField resignFirstResponder];
    NSMutableArray * titles = [NSMutableArray new];
    for (STBankModel * model in self.banksArray) {
        [titles addObject:model.bank];
    }
    STPickerViewController * picker = [[STPickerViewController alloc] initWithPickerArray:@[titles] andWithHandle:^(NSArray<NSString *> *stringArray) {
        NSInteger index = [titles indexOfObject:stringArray.firstObject];
        self.chosedBankModel = self.banksArray[index];
        [self.bankButton setTitle:self.chosedBankModel.bank forState:UIControlStateNormal];
    }];
    [self presentViewController:picker animated:NO completion:nil];
}
- (void)sendAddbankRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@",service_header,@"/gold/public/api/personal/bangCard"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if ([STUserManger defult].token.length) {
        [dic setObject:[STUserManger defult].token forKey:@"token"];
    }
    if (self.idCardTextField.text.length) {
        [dic setObject:self.idCardTextField.text forKey:@"bank_card"];
    }
    if (self.zhihangTextField.text.length) {
        [dic setObject:self.zhihangTextField.text forKey:@"bank"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                
                                                 [TMUtils fetchPersonnalInfo:^(TMGUserModel *user) {
                                                     [[STUserManger defult] updateUserModel:user];
                                                      [SVProgressHUD showSuccessWithStatus:@"添加成功"];
                                                     [self.navigationController popViewControllerAnimated:YES];
                                                 }];
                                                 
                                                 
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
- (void)sendfetchBanksRequest{
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if ([STUserManger defult].token.length) {
        [dic setObject:[STUserManger defult].token forKey:@"token"];
    }
    NSString * url = [NSString stringWithFormat:@"%@%@",service_header,@"/gold/public/api/personal/getBanks"];
    
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 [self.tableView.mj_header endRefreshing];
                                                 NSArray * dataArray =responseObject[@"data"];
                                                 self.banksArray = [NSMutableArray new];
                                                 for (NSDictionary* dataDic in dataArray) {
                                                     STBankModel * model = [STBankModel mj_objectWithKeyValues:dataDic];
                                                     [self.banksArray addObject:model];
                                                 }
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {

                                                 [SVProgressHUD dismiss];
                                             }];
}
@end
